package com.atguigu.system.handler;

import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.system.exception.GuiguException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.AccessDeniedException;


@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result resoveExcetion(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    //创建一个处理数学异常的方法
    @ExceptionHandler(ArithmeticException.class)
    public Result resoveArithmeticExcetion(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage());
    }

    //处理自定义异常的方法
    @ExceptionHandler(GuiguException.class)
    public Result resoveGuiguException(GuiguException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }
}
